package cn.edu.xmut.chen.zhlg.common.framework.utils;

import cn.edu.xmut.chen.zhlg.common.framework.exception.AssertionException;
import cn.edu.xmut.chen.zhlg.common.utils.ResultType;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.regex.Pattern;

public class AssertUtils {

    public static void assertNull(Object target, ResultType resultType, String message) {
        if (target != null) {
            throwAssertionException(resultType, message);
        }
    }

    public static void assertNotNull(Object target, ResultType resultType, String message) {

        if (target == null) {
            throwAssertionException(resultType, message);
        }
    }

    public static void assertColletionNotNull(Collection<?> collection, ResultType resultType, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            throwAssertionException(resultType, message);
        }
    }

    public static void assertTrue(boolean target, ResultType resultType, String message) {
        if (!target) {
            throwAssertionException(resultType, message);
        }
    }

    public static void assertFalse(boolean target, ResultType resultType, String message) {
        assertTrue(!target, resultType, message);
    }

    public static void assertHasText(String target, ResultType resultType, String message) {
        if (StringUtils.isBlank(target)) {
            throwAssertionException(resultType, message);
        }
    }

    public static void assertMatches(String target, Pattern pattern, ResultType resultType, String message) {
        if (!pattern.matcher(target).matches()) {
            throwAssertionException(resultType, message);
        }
    }

    private static void throwAssertionException(ResultType resultType, String message) {
        throw new AssertionException(resultType, message);
    }

    private AssertUtils() {
    }
}
